package com.neuedu.springmvc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ValidateLoginInterceptor implements HandlerInterceptor {

    /**
     * 在控制器方法之前执行的
     * @param request
     * @param response
     * @param handler
     * @return   false：控制的方法不会就行执行，同时postHandle、afterCompletion 方法也都不会继续执行
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("登录的拦截器:preHandle");

        //根据session状态，判断是否执行，
        String isLogin = request.getParameter("isLogin");

        //如果没有登录，通过response返回结果


        return isLogin!=null && isLogin.length()>0;
    }

    /**
     * 在控制器方法之后执行 ,如果有异常不会执行
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("登录的拦截器:postHandle");

    }

    /**
     * 在控制器方法之后执行 有异常也正常的执行
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("登录的拦截器:afterCompletion");
    }
}